<?php


/**
 * 绑定银行卡控制器
 * @author  yw
 */


class Card extends Web_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Card_model');
	}
	//银行卡列表
	public function index()
	{
		$data['base_url'] = $this->base_url;
		$this->loadView('header', $data);
		$this->loadView('card');
	}
	//绑定银行卡
	public function addcard()
	{
		$this->load->model('User_model');
		$user = $this->userSession(true, 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		if($_POST)
		{
			$param = $this->input->post();
			foreach ($param as $k => $v) {
				if(empty($v) && $k != 'subbranch'){
					echo json_encode(array('result'=>'10001','errmsg'=>'参数异常'));return;
				}
			}
			$bankname = $this->Card_model->getBankNameByBankcode($param['bankcode']);
			$bankinfo = $param['bankname'].'-'.$param['city'].'-'.$bankname['banktypename'];//银行信息
			$data = $this->User_model->bindBank($user->uid, $param['card'],$param['subbranch'], $param['username'],$bankinfo);
			if($data!==false)
			{
				echo json_encode(array('result'=>0,'resultmsg'=>'添加成功'));
			}
			return;
		}
		$userinfo = $this->User_model->checkCard($user->uid);
		if(!empty($userinfo['cardid']) && $this->input->get('status')!='edit')
		{
			header('Location:'.$this->base_url.'home/card/cardlist');return;
		}
		$banklist = $this->Card_model->getBankList();
		$data['banklist'] = $banklist;
		$data['base_url'] = $this->base_url;
		$data['user'] = $user;
		$this->loadView('header', $data);
		$this->loadView('bind_card');
	}
	//银行卡列表
	public function cardList()
	{
		$user = $this->userSession(true, 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		$this->load->model('User_model');
		$userinfo = $this->User_model->checkCard($user->uid);
		$arr =  explode('-',$userinfo['bankinfo']);
		$data['bankname'] = $arr[2];
        $data['user'] = $user;
		$data['userinfo'] = $userinfo;
		$data['base_url'] = $this->base_url;
		$this->loadView('header',$data);
		$this->loadView('card',$data);
	}
	//获取省份
	public function getProvince()
	{
		$bankcode = $this->input->get('bankcode');
		$banklist = $this->Card_model->getProvince($bankcode);
		echo json_encode(array('result'=>0,'datas'=>$banklist));
	}
	//获取城市
	public function getCity()
	{
		$bankcode = $this->input->get('bankcode');
		$bankname = $this->input->get('bankname');
		$data = $this->Card_model->getCity($bankcode,$bankname);
		echo json_encode(array('result'=>0,'datas'=>$data));
	}
	//获取支行
	public function getSubbranch()
	{
		$bankcode = $this->input->get('bankcode');
		$bankname = $this->input->get('bankname');
		$cityname = $this->input->get('cityname');
		$data = $this->Card_model->getSubbranch($bankcode,$bankname,$cityname);
		echo json_encode(array('result'=>0,'datas'=>$data));
	}
}